<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/6
 * Time: 10:00
 */

namespace app\admin\controller\store;


use app\admin\controller\BaseController;
use app\admin\model\Position;
use app\admin\model\StoreInfo;
use app\admin\model\User;
use think\Session;
use think\Controller;

class StayIn extends BaseController
{
    public function page(){
        $storeInfo=new StoreInfo();
        $user=(new Session())->get("user");
        if($user["id"]){
            $items=$storeInfo->where([
                "status"        =>0,
                "is_del"        =>0,
                "menber_id"     =>$user["id"]
            ])->paginate(15);
        }else{
            $items=$storeInfo->where([
                "status"        =>0,
                "is_del"        =>0
            ])->paginate(15);
        }

        $page=$items->render();
        $this->assign("items",$items);
        $this->assign("page",$items);
        return $this->fetch("index");
    }

    public function edit(){
        $storeInfo=new StoreInfo();
        $position=new Position();
        $user=new User();
        if($this->request->isGet()){
            if($this->request->has("id")){//编辑
                $id=$this->request->get("id");
                $item=$storeInfo->find([
                    "id"    =>$id
                ]);
                //dump($item);die();
                if($this->request->has("action")){
                    $this->assign("controller","store.apply");
                }
                $cities=$position->getCityOrArea($item->province);
                $areas=$position->getCityOrArea($item->city);
                $this->assign("cities",$cities);
                $this->assign("areas",$areas);
                $this->assign("item",$item);
            }else{//添加

            }
            $users=$user->select();
            $provinces=$position->getProvince();
            $this->assign("users",$users);
            $this->assign("provinces",$provinces);
            return $this->fetch();
        }else {
            $data = $this->request->param();
            //dump($data);die();
            if (!empty($data["id"])) {//更新
                $storeInfo::update($data);
                return json([
                    "code" => 200,
                    "msg" => "update success"
                ]);
            } else {//插入
                unset($data["id"]);
                $storeInfo->save($data);
                return json([
                    "code" => 200,
                    "msg" => "insert success"
                ]);
            }
        }
    }

    public function del(){
        $storeInfo=new StoreInfo();
        $data=$this->request->param();
        $data["is_del"]=1;
        $res=$storeInfo::update($data);
        if($res){
            $this->success("success");
        }
        $this->error("success");
    }

    public function getCity(){
        $postion=new Position();
        $code=$this->request->param("code");
        $data=$postion->getCityOrArea($code);
        if(!$data){
            return json([
                "code"      =>3000,
                "errmsg"    =>"failed"
            ]);
        }else{
            return json([
                "code"      =>200,
                "msg"       =>"success",
                "data"      =>$data
            ]);
        }
    }
}